package com.smartx.app.util.libimage;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;

import java.io.File;
import java.lang.ref.WeakReference;

/**
 * @author: tangjie
 * @date: 2017/3/28 9:29
 * @Description:
 */

public class GlideStrategy implements BaseImageLoaderStrategy {
    private final RequestManager mRequestManager;
    private Context mContext;

    public GlideStrategy(Context context) {
        mContext = context;
        GlideDownloadProgress.init(Glide.get(context));
        mRequestManager = Glide.with(context);
    }

    @Override
    public void displayImage(String imageUrl, ImageView imageView) {
        Glide.with(mContext).load(imageUrl).into(imageView);
    }

    @Override
    public void displayImage(String imageUrl, final ImageView imageView, ImageConfig imageConfig) {
        DrawableTypeRequest<String> request = Glide.with(mContext).load(imageUrl);
        if (imageConfig.getImageResOnLoading() > 0) {
            request.placeholder(imageConfig.getImageResOnLoading());
        }
        if (imageConfig.getImageResOnFail() > 0) {
            request.error(imageConfig.getImageResOnFail());
        }
        if (imageConfig.getRoundCornerRadius() > 0) {
            request.transform(new GlideRoundTransform(mContext, imageConfig.getRoundCornerRadius()));
        }
        if (imageConfig.getFadeInTime() > 0) {
            request.crossFade(imageConfig.getFadeInTime());
        }
        if (ImageConfig.SCALE_FIT_IMAGE == imageConfig.getScaleType()) {
            request.override(ViewTarget.SIZE_ORIGINAL, ViewTarget.SIZE_ORIGINAL);
        }
        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<>(imageView);
        ImageView target = imageViewWeakReference.get();
        if (target != null) {
            request.dontAnimate().into(imageView);
        }
    }

    @Override
    public void loadImage(String uri, ImageConfig optionsImage, final Callback callback) {
        mRequestManager.load(uri)
                .downloadOnly(new ImageDownloadTarget(uri.toString()) {
                    @Override
                    public void onResourceReady(File image,
                                                GlideAnimation<? super File> glideAnimation) {
                        if (callback != null) {
                            callback.onLoadingComplete(image);
                        }
                    }

                    @Override
                    public void onDownloadStart() {
                        if (callback != null) {
                            callback.onStart();
                        }
                    }

                    @Override
                    public void onProgress(int progress) {
                        if (callback != null) {
                            callback.onProgress(progress);
                        }
                    }

                    @Override
                    public void onDownloadFinish() {
                        callback.onFinish();
                    }

                    @Override
                    public void onDownloadFail() {
                        callback.onLoadingFailed();
                    }
                });
    }

    @Override
    public void pauseRequests() {
        Glide.with(mContext).pauseRequests();
    }

    @Override
    public void resumeRequests() {
        Glide.with(mContext).resumeRequests();
    }

}
